<template>
  <div class="app-container" v-loading="loading">
    <div style="margin:0 0 5px 20px">
      Fill in the category link of jumia
    </div>
    <div>
      <el-form :inline="true"  class="demo-form-inline">
        <el-form-item label="jumia类目链接">
          <el-input v-model="jumiaLink" placeholder=""></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit">submit</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :key="tableKey"
      v-loading="loading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" prop="id" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Date" width="150px" align="center">
        <template slot-scope="{row}">
          <span></span>
        </template>
      </el-table-column>

      <el-table-column label="Author" width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.author }}</span>
        </template>
      </el-table-column>

      <el-table-column label="Imp" width="80px">
        <template slot-scope="{row}">
          <svg-icon v-for="n in + row.importance" :key="n" icon-class="star" class="meta-item__icon" />
        </template>
      </el-table-column>
      <el-table-column label="Readings" align="center" width="95">
        <template slot-scope="{row}">
          <span v-if="row.pageviews" class="link-type" @click="handleFetchPv(row.pageviews)">{{ row.pageviews }}</span>
          <span v-else>0</span>
        </template>
      </el-table-column>

      <el-table-column label="Actions" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            Edit
          </el-button>
          <el-button v-if="row.status!='published'" size="mini" type="success" @click="handleModifyStatus(row,'published')">
            Publish
          </el-button>
          <el-button v-if="row.status!='draft'" size="mini" @click="handleModifyStatus(row,'draft')">
            Draft
          </el-button>
          <el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDelete(row,$index)">
            Delete
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>

import { importJumiaCategoryProducts,fetchList } from "@/api/palmbuy/jumia"
import Pagination from '@/components/Pagination'

export default {
  components: { Pagination },
  data() {
    return {
      loading:false,
      jumiaLink: '',
      tableKey: 0,
      list: null,
      total: 0,
      listQuery: {
        page: 1,
        limit: 50,
        importance: undefined,
        title: undefined,
        type: undefined,
      },
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    onSubmit() {
      this.loading = true
      importJumiaCategoryProducts({categoryId:this.jumiaLink}).then((res)=>{
        console.log(res.data)
        this.loading = false
      })
    },
    getList() {
      this.loading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.loading = false
        }, 1.5 * 1000)
      })
    },
  }
}
</script>

<style lang="scss">

</style>
